<template>
  <div>
    <!-- 面包屑 区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 导航栏 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoodItem">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="expand"></el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          width="100px"
          prop="goods_price"
        ></el-table-column>
        <el-table-column
          label="商品数量"
          prop="goods_number"
          width="100px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="100px">
          <!-- 时间想渲染成自己想要的样子 -->
          <template v-slot="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" circle ></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteGoodItem(scope.row.goods_id)" circle ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 当前页数的数据数组
      goodsList: [],
      // 数据总条数
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.errror('获取数据失败')
      }
      console.log(res.data)
      // 对获取的数据赋值
      this.goodsList = res.data.goods
      // 总条数
      this.total = res.data.total
    },
    // 当每页个数发生改变时
    handleSizeChange (newPage) {
      this.queryInfo.pagesize = newPage
      this.getGoodsList()
    },
    // 当前页码发生改变
    handleCurrentChange (newpagenum) {
      this.queryInfo.pagenum = newpagenum
      this.getGoodsList()
    },
    // 删除商品
    deleteGoodItem (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        }
        this.getGoodsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加商品
    addGoodItem () {
      // 使用hash的push跳转 不会刷新
      this.$router.push('/good')
    }
  }
}
</script>
<style scoped>
.el-card {
  margin-top: 10px;
}
</style>
